<template>
  <el-card style="margin: 20px">
    <el-row type="flex" justify="space-between">
      <el-col :span="12">
        <el-row type="flex" justify="start">
          <div style="display: flex">
            标签名称
            <el-input v-model="params.tagName"></el-input>
          </div>
          <div style="display: flex">
            状态
            <el-select v-model="params.state" placeholder="请选择">
              <el-option label="禁用" value="0"></el-option>
              <el-option label="启用" value="1"></el-option>
            </el-select>
          </div>

          <el-button style="margin-left: 10px">清空</el-button>
          <el-button type="primary" @click="search">搜索</el-button>
        </el-row>
      </el-col>
      <el-col :span="2">
        <el-button type="success" class="el-icon-edit" @click="adduser"
          >新增标签</el-button
        >
      </el-col>
    </el-row>
    <el-alert
      style="margin-top: 20px"
      :title="`共${counts}条记录`"
      type="info"
      :closable="false"
      show-icon
    >
    </el-alert>

    <el-table style="margin-top: 20px" :data="userlist">
      <el-table-column type="index" label="序号" width="80"> </el-table-column>
      <el-table-column prop="subjectName" label="所属学科" width="80">
      </el-table-column>
      <el-table-column prop="tagName" label="标签名称" width="180">
      </el-table-column>
      <el-table-column prop="username" label="创建者" width="80">
      </el-table-column>
      <el-table-column prop="addDate" label="创建日期" width="280">
      </el-table-column>
      <el-table-column prop="state" label="状态" width="80"> </el-table-column>
      <el-table-column prop="date" label="操作" width="280">
        <template #default="{ row }">
          <el-link type="primary" style="margin: 0 10px">禁用</el-link>
          <el-link
            type="primary"
            style="margin: 0 10px"
            @click="change(row, row.id)"
            >修改</el-link
          >
          <el-link
            type="primary"
            style="margin: 0 10px"
            @click="removeUser(row, row.id)"
            >删除</el-link
          >
        </template>
      </el-table-column>
    </el-table>
    <div class="block" style="margin: 25px">
      <el-pagination
        background
        @size-change="params.pagesize"
        @current-change="handleCurrentChange"
        :current-page.sync="currentPage3"
        :page-sizes="[10, 20, 30, 50]"
        layout="total, sizes, prev, pager, next, jumper"
        :total="counts"
      >
      </el-pagination>
    </div>

    <!-- 弹框 -->

    <el-dialog
      @close="handleClose"
      :title="title"
      :visible.sync="dialogVisible"
      width="40%"
      :before-close="handleClose"
    >
      <div v-if="addView">
        <el-form v-model="form" label-width="120px" ref="formRef">
          <el-form-item label="所属学科">
            <el-select v-model="form.subjectID" placeholder="请选择活动区域">
              <el-option
                v-for="item in options"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="目录名称">
            <el-input v-model="form.tagName"></el-input>
          </el-form-item>
        </el-form>
      </div>

      <div v-if="changeView">
        <el-form v-model="form" label-width="120px" ref="formRef">
          <el-form-item label="所属学科">
            <el-select v-model="form.subjectID" placeholder="请选择活动区域">
              <el-option
                v-for="item in options"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="目录名称">
            <el-input v-model="form.tagName"></el-input>
          </el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submit">确 定</el-button>
      </span>
    </el-dialog>
  </el-card>
</template>

<script>
import { list, add, simple, remove, update } from '@/api/hmmm/tags'
export default {
  data() {
    return {
      value: '',
      id: '',
      changeView: false,
      title: '',
      addView: false,
      options: [],
      keyword: '',
      form: {
        tagName: '',

        subjectID: '',
        totals: '',
        tags: '',
        username: '',
        addDate: '',
        isFrontDisplay: ''
      },
      dialogVisible: false,
      currentPage3: null,
      username: '',
      pages: '',
      input: '',
      counts: null,
      userlist: [],
      params: {
        page: 1,
        pagesize: 10,
        tagName: '',
        state: ''
      }
    }
  },
  methods: {
    async list() {
      const { data } = await list(this.params)

      this.userlist = data.items

      this.counts = data.counts
      this.pages = data.pages

      this.userlist.forEach((item) => {
        if (item.state === 1) {
          item.state = '已启用'
        } else {
          item.state = '已禁用'
        }
      })
    },
    handleCurrentChange(current) {
      this.params.page = current
      this.list()
    },

    async search() {
      if (this.params.state === '0') {
        this.params.state = 0
      } else {
        this.params.state = 1
      }
      await list({
        page: this.params.page,
        pagesize: this.params.pagesize,
        state: this.params.state,
        tagName: this.params.tagName
      })
      console.log(this.params.state)
      this.list()
    },

    async adduser() {
      this.dialogVisible = true
      this.addView = true
      this.changeView = false
      this.title = '新增目录'
      const { data } = await simple()
      this.options = data
      console.log(this.options)
    },

    async change(row) {
      console.log(row)
      this.form = row
      this.id = row.id
      this.form.isFrontDisplay = row.isFrontDisplay

      this.dialogVisible = true
      this.addView = false
      this.changeView = true
      this.title = '修改目录'
      const { data } = await simple()
      this.options = data
    },

    async removeUser(row, id) {
      console.log(id)
      const res = await remove(row, id)
      this.$message.success('删除用户成功')
      console.log(res)
      this.list()
    },

    handleClose() {
      this.dialogVisible = false
      this.$refs.formRef.resetFields()
      this.form = {}
    },
    async submit() {
      if (this.title === '新增目录') {
        await add({
          id: null,
          tagName: this.form.tagName,
          subjectID: this.form.subjectID
        })
        this.$message.success('新增目录成功')
      } else {
        await update({
          id: this.id,
          tagName: this.form.tagName,
          subjectID: this.form.subjectID
        })
        this.$message.success('修改目录成功')
      }
      this.dialogVisible = false
      this.list()
    }
  },
  created() {
    this.list()
  }
}
</script>

<style scoped lang="scss"></style>
